using System;
using System.Collections.Generic;

namespace VisionarySystems.TimeRegi.Model.TimePeriods
{
    public class TimePeriodModel : TimePeriodHost<int>, ITimePeriode
    {

        #region private members

        //Dictionary<int, WorkYear> m_Years = new Dictionary<int, WorkYear>();

        #endregion

		public TimePeriodModel()
		{
			ID = new TimePeriodeID(GetUniqueIndex(), TimePeriodeType.Controller);			
		}
        #region properties

        public int Year
        {
            get { throw new NotImplementedException(); }
        }
		
        #endregion

        #region Public Methods

        public TimeSpan BrakeTime { get; set; }

        public void Add(IEnumerable<TimeSlot> TimeSlots)
        {
            foreach (TimeSlot slot in TimeSlots)
            {
                ITimePeriode currentYear;
                if (!m_TimePeriods.TryGetValue(slot.Year, out currentYear))
                {
                    currentYear = new WorkYear(slot.Year);
                    m_TimePeriods.Add(currentYear.Year, currentYear);
					//currentYear.ID = GetNextID(currentYear);
                }
                currentYear.Add(slot);
            }
        }

        public IEnumerable<TimeSlot> TimeSlots()
        {
            foreach (ITimePeriode year in m_TimePeriods.Values)
                foreach (ITimePeriode week in year as WorkYear)
                    foreach (ITimePeriode day in week as WorkWeek)
                        foreach (ITimePeriode slot in day as WorkDay)
                            yield return slot as TimeSlot;
        }

        public void GetTimePeriode(DateTime StartTime, DateTime EndTime, ref ITimePeriode TimePeriod)
        {
            foreach (var year in m_TimePeriods.Values)
            {
                year.GetTimePeriode(StartTime, EndTime, ref TimePeriod);
            }
        }

        public void GetWorkingDates(ref List<DateTime> WorkingDates)
        {
            foreach (var year in m_TimePeriods.Values)
                year.GetWorkingDates(ref WorkingDates);
        }

	    public bool DeleteTimeslots(ref List<int> RemovedIndexes)
	    {
			bool result = false;
			foreach (var periode in m_TimePeriods.Values)
				result |= periode.DeleteTimeslots(ref RemovedIndexes);
			return result;

	    }

	    #endregion

        public ITimePeriode GetTimePeriode(int p)
        {
            throw new NotImplementedException();
        }

	    public void DeleteTimePeriode(ITimePeriode Periode)
	    {
		    WorkYear deleteWeek = Periode as WorkYear;
		    if (deleteWeek != null)
		    {
			    if (m_TimePeriods.ContainsKey(deleteWeek.Year))
				    m_TimePeriods.Remove(deleteWeek.Year);
		    }
		    else
		    {
			    foreach (var year in m_TimePeriods.Values)
				    year.DeleteTimePeriode(Periode);
		    }
	    }

	    public void DeleteTimeSlot(int Idx)
	    {
			foreach (var periode in m_TimePeriods.Values)
				periode.DeleteTimeSlot(Idx);
		}
    }
}